package com.hushao.aimi.service.impl;

import com.hushao.aimi.vo.AddressVo;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiAddress;
import com.hushao.aimi.entity.AimiUser;
import com.hushao.aimi.repository.AddressRepository;
import com.hushao.aimi.repository.UserRepository;
import com.hushao.aimi.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description: 地址服务
 * @author: 胡少
 * @create: 2018-06-18 12:11
 **/
@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressRepository addressRepository;

    @Autowired
    private UserRepository userRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServerResponse add(String username, AddressVo addressVo) throws Exception {
        AimiUser user=userRepository.findByUsername(username);
        if(user==null){
            return ServerResponse.Success("添加地址失败！没有此用户");
        }
        AimiAddress aimiAddress=new AimiAddress(user.getId(),addressVo.getReceiverName(),
                addressVo.getReceiverPhone(),
                addressVo.getReceiverProvince(),
                addressVo.getReceiverCity(),
                addressVo.getReceiverDistrict(),
                addressVo.getReceiverAddress(),
                addressVo.getReceiverZipCode(),
                addressVo.getAreaCode(),
                addressVo.getComment());
        AimiAddress a=addressRepository.save(aimiAddress);
        log.info("添加：{}的用户地址成功！",username);
        return ServerResponse.Success("添加地址成功！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServerResponse add(String addressId) throws Exception {
        addressRepository.deleteById(addressId);
        log.info("删除地址成功：{}",addressId);
        return ServerResponse.Success("删除地址成功！");
    }

    @Override
    public ServerResponse update(String username, AddressVo addressVo) throws Exception {
        AimiUser user=userRepository.findByUsername(username);
        if(user==null){
            return ServerResponse.Success("修改地址失败！没有此用户");
        }
        AimiAddress aimiAddress=new AimiAddress(addressVo.getId(),
                user.getId(),
                addressVo.getReceiverName(),
                addressVo.getReceiverPhone(),
                addressVo.getReceiverProvince(),
                addressVo.getReceiverCity(),
                addressVo.getReceiverDistrict(),
                addressVo.getReceiverAddress(),
                addressVo.getReceiverZipCode(),
                addressVo.getAreaCode(),
                addressVo.getComment());
        addressRepository.save(aimiAddress);
        log.info("修改地址成功:{}",username);
        return ServerResponse.Success("修改地址成功！");
    }

    @Override
    public ServerResponse detail(String addressId) {
        Optional<AimiAddress> optional=addressRepository.findById(addressId);
        if (optional.isPresent()){
            AimiAddress o=optional.get();
            AddressVo addressVo=new AddressVo(o.getId(),
                    o.getReceiverName(),
                    o.getReceiverPhone(),
                    o.getReceiverProvince(),
                    o.getReceiverCity(),
                    o.getReceiverDistrict(),
                    o.getReceiverAddress(),
                    o.getReceiverZipCode(),
                    o.getAreaCode(),
                    o.getComment());
            return ServerResponse.Success(addressVo);

        }
        log.error("无法查询地址：{}",addressId);
        return ServerResponse.Error("无法查询该地址信息");

    }

    @Override
    public ServerResponse listByUsername(String username) {
        AimiUser user=userRepository.findByUsername(username);
        List<AimiAddress> addresses=addressRepository.findByUserId(user.getId());
        List<AddressVo> addressVos= addresses.stream().map(o->new AddressVo(o.getId(),
                o.getReceiverName(),
                o.getReceiverPhone(),
                o.getReceiverProvince(),
                o.getReceiverCity(),
                o.getReceiverDistrict(),
                o.getReceiverAddress(),
                o.getReceiverZipCode(),
                o.getAreaCode(),
                o.getComment())).collect(Collectors.toList());
        return ServerResponse.Success(addressVos);
    }


}
